<?php
// +----------------------------------------------------------------------
// | 中联致远服务端框架
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.zhongliansoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tony <tanyu@zhongliansoft.com>
// +----------------------------------------------------------------------

namespace app\Manage\controller;

use app\common\controller\Manage;
use app\common\model\Common;
use app\common\model\BookReturn as BookReturnsModel;
use app\common\model\Config as ConfigModel;
use app\common\model\BookAccount as BookAccountModel;
use app\common\model\BookAdjust as BookAdjustModel;
use app\common\model\ViewGetDepts as ViewGetDeptsModel;
use app\common\model\ViewGetYears as ViewGetYearsModel;
use app\common\model\ViewGetSeasons as ViewGetSeasonsModel;
use app\common\model\ViewGetDirs as ViewGetDirsModel;
use app\common\model\BookBatchDraw as BookBatchDrawsModel;
use app\common\model\Config as ConfigsModel;
use think\facade\Request;
use think\Db;
use think\facade\Log;

class BookReturn extends Manage
{
    // 教材预发放（退书）
    public function drawreturnindex()
    {   
        if(Request::isAjax())
        {
            $bookbatchdrawModel = new BookBatchDrawsModel();
            return $bookbatchdrawModel->tablereturnData(input('param.'));
        }

        //获取年度列表
        $viewGetYearsModel = new ViewGetYearsModel();
        $yearList = $viewGetYearsModel->select();
        $this->assign('yearList', $yearList);

        //获取季度列表
        $viewGetSeasonsModel = new ViewGetSeasonsModel();
        $seasonList = $viewGetSeasonsModel->select();
        $this->assign('seasonList', $seasonList);

        //获取部门列表
        $viewGetDeptsModel = new ViewGetDeptsModel();
        $deptList = $viewGetDeptsModel->select();
        $this->assign('deptList', $deptList);
        
        //获取方向列表
        $ViewGetDirsModel = new ViewGetDirsModel();
        $dirList = $ViewGetDirsModel->select();
        $this->assign('dirList', $dirList);
        
        // 默认值
        $ConfigsModel = new ConfigsModel();
        $year = $ConfigsModel->getYear();
        $season = $ConfigsModel->getSeason();
        $this->assign('year', $year);
        $this->assign('season', $season);
        $this->assign('userId', session('manage.id'));
        return $this->fetch("/book_draw_return/index");
    }


    // 教材预发放（退书）：退书
    public function drawreturnreturn()
    {
        $this->view->engine->layout(false);
        $bookbatchdrawModel = new BookBatchDrawsModel();
        if(Request::isPost())
        {
            return $bookbatchdrawModel->predrawReturn(input('param.'));
        }

        //获取年度列表
        $viewGetYearsModel = new ViewGetYearsModel();
        $yearList = $viewGetYearsModel->select();
        $this->assign('yearList', $yearList);

        //获取季度列表
        $viewGetSeasonsModel = new ViewGetSeasonsModel();
        $seasonList = $viewGetSeasonsModel->select();
        $this->assign('seasonList', $seasonList);

        //获取部门列表
        $viewGetDeptsModel = new ViewGetDeptsModel();
        $deptList = $viewGetDeptsModel->select();
        $this->assign('deptList', $deptList);
        
        //获取方向列表
        $ViewGetDirsModel = new ViewGetDirsModel();
        $dirList = $ViewGetDirsModel->select();
        $this->assign('dirList', $dirList);

        // 获取退书原因列表
        $condition = [["num", "lt", 0]];  # 必须是二维数组
        $reasonList = $bookbatchdrawModel->distinct(true)->field('return_reason')->where($condition)->select();
        $this->assign('reasonList', $reasonList);
        
        // 默认值
        $ConfigsModel = new ConfigsModel();
        $year = $ConfigsModel->getYear();
        $season = $ConfigsModel->getSeason();
        $this->assign('year', $year);
        $this->assign('season', $season);
        return $this->fetch('/book_draw_return/return');
    }

    /**
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        if(Request::isAjax())
        {
            $bookReturnModel = new BookReturnsModel();
            return $bookReturnModel->tableData(input('param.'));
        }

        //获取部门列表
        $viewGetDeptsModel = new ViewGetDeptsModel();
        $dep_nameList = $viewGetDeptsModel->select();
        $this->assign('depList', $dep_nameList);

        //获取年度列表
        $viewGetYearsModel = new ViewGetYearsModel();
        $year_List = $viewGetYearsModel->select();
        $this->assign('yearList', $year_List);

        //获取季度列表
        $viewGetSeasonsModel = new ViewGetSeasonsModel();
        $season_List = $viewGetSeasonsModel->select();
        $this->assign('seasonList', $season_List);
        
        //获取当前年度季度
        $ConfigsModel = new ConfigsModel();
        $year = $ConfigsModel->getYear();
        $season = $ConfigsModel->getSeason();
        $this->assign('curr_year', $year);
        $this->assign('curr_season', $season);
        $this->assign('userId', session('manage.id'));

        return $this->fetch();
    }

    // 教材明细
    public function bookreturnbookdetail()
    { 
        $this->view->engine->layout(false);
        return $this->fetch("/book_return/bookdetail", ['id' => input('id')]);
    }

    // 教材明细
    public function getBooReturnBookDetailList()
    {  
        $bookReturnsModel = new BookReturnsModel();
        return $bookReturnsModel->getBookDetail(input('param.')); 
    }

    /**
     *  退书
     * @return array|mixed
     */
    public function bookReturn()
    { 
        if(Request::isPost())
        {
            $bookReturnsModel = new BookReturnsModel();
            return $bookReturnsModel->bookReturn(input('param.'));
        }
 
        $config = new ConfigModel();
        $year = $config->getYear();
        $season = $config->getSeason();
        $this->assign('curr_year', $year);
        $this->assign('curr_season', $season);

        return $this->fetch('/book_return/book_return');
    }

    // 退书删除
    public function bookreturndel()
    { 
        $returnId = input('param.')['returnId'];

        $common = new Common();
        $common->query("call bookReturnDel('".$returnId."');");
         
        $return_data = array(
            'status' => true,
            'msg'    => '删除成功',
            'data'   => []
        );

        return $return_data;
    }
    
    // 计划调整
    public function bookAdjustIndex()
    {   
        if(Request::isAjax())
        {
            $BookAdjustModel = new BookAdjustModel();
            return $BookAdjustModel->tableData(input('param.'));
        }

        //获取部门列表
        $viewGetDeptsModel = new ViewGetDeptsModel();
        $dep_nameList = $viewGetDeptsModel->select();
        $this->assign('depList', $dep_nameList);

        //获取年度列表
        $viewGetYearsModel = new ViewGetYearsModel();
        $year_List = $viewGetYearsModel->select();
        $this->assign('yearList', $year_List);

        //获取季度列表
        $viewGetSeasonsModel = new ViewGetSeasonsModel();
        $season_List = $viewGetSeasonsModel->select();
        $this->assign('seasonList', $season_List);
        
        //获取方向列表
        $ViewGetDirsModel = new ViewGetDirsModel();
        $dir_List = $ViewGetDirsModel->select();
        $this->assign('dirList', $dir_List);

        //获取当前年度季度
        $ConfigsModel = new ConfigsModel();
        $year = $ConfigsModel->getYear();
        $season = $ConfigsModel->getSeason();
        $this->assign('curr_year', $year);
        $this->assign('curr_season', $season);
        $this->assign('userId', session('manage.id'));
 
        return $this->fetch("/book_adjust/index");
    }

    /**
     *  计划调整增加
     * @return array|mixed
     */
    public function bookAdjust()
    {
        if(Request::isPost())
        {
            $BookAdjustModel = new BookAdjustModel();
            return $BookAdjustModel->bookAdjust(input('param.'));
        }
 
        $config = new ConfigModel();
        $year = $config->getYear();
        $season = $config->getSeason();
        $this->assign('curr_year', $year);
        $this->assign('curr_season', $season);

        return $this->fetch('/book_adjust/book_adjust');
    }
 
    /**
     *  获取所有图书列表
     * @return array|mixed
     */
    public function getExtraBookList()
    {
        $xh  = input('param.xh', '');     // 搜索关键字
        $limit = input('limit', 0); 
        $page = input('page', 0);
        $index = ($page - 1) * $limit;

        $model = new Common();
        $list = $model->query("call getExtraBookList('".$xh."',".$index.",".$limit.");");
        
        $re['code'] = 0;
        $re['msg'] = '';
        $re['count'] = count($list[1]);
        $re['data'] = $list[0];
        
        return $re;
    }
    
    // 计划调整：删除
    public function bookAdjustDel()
    { 
        $adjustId = input('param.')['adjustId'];

        $bookAdjustModel = new BookAdjustModel();
        $bookAdjustInfo = $bookAdjustModel->where(['id'=>$adjustId])->find();
        if(!$bookAdjustInfo) {
            $return_data = array(
                'status' => true,
                'msg'    => '没有这条记录',
                'data'   => []
            );
            return $return_data;
        }

        $model = new Common();
        $model->query("call bookAdjustDel('".$adjustId."');");
         
        // 设置账表状态
        // $config = new ConfigModel();
        // $year = $config->getYear();
        // $season = $config->getSeason();
        // $bookAccountModel = new BookAccountModel();
        // $bookAccountModel->save(['status'=>0], ['year'=>$year, 'season'=>$season, 'xh'=>$bookAdjustInfo['xh']]);
        
        $return_data = array(
            'status' => true,
            'msg'    => '删除成功',
            'data'   => []
        );

        return $return_data;
    }

    /**
     * 导入用户
     * @return array|mixed
     */
    public function ImportBookAdjust()
    {
        $config = new ConfigModel();
        $year = $config->getYear();
        $season = $config->getSeason();
        $this->assign('curr_year', $year);
        $this->assign('curr_season', $season);

        return $this->fetch('/book_adjust/ImportBookAdjust');
    }

}